home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 August: Tool Chest / Dev.CD Aug 00 TC Disk 2.toast / pc / sample code / human interface toolbox / fragment tool / streams.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-06-23  |  4.4 KB  |  225 lines

  1. /*
  2.     File:        Streams.c
  3.  
  4.     Contains:    Utility routines to stream data into a block of memory
  5.  
  6.     Written by: Chris White    
  7.  
  8.     Copyright:    Copyright © 1995-1999 by Apple Computer, Inc., All Rights Reserved.
  9.  
  10.                 You may incorporate this Apple sample source code into your program(s) without
  11.                 restriction. This Apple sample source code has been provided "AS IS" and the
  12.                 responsibility for its operation is yours. You are not permitted to redistribute
  13.                 this Apple sample source code as "Apple sample source code" after having made
  14.                 changes. If you're going to re-distribute the source, we require that you make
  15.                 it clear in the source that the code was descended from Apple sample source
  16.                 code, but that you've made changes.
  17.  
  18.     Change History (most recent first):
  19.                 8/5/1999    Karl Groethe    Updated for Metrowerks Codewarror Pro 2.1
  20.                 
  21.  
  22. */
  23.  
  24.  
  25. #ifndef __TYPES__
  26.     #include <Types.h>
  27. #endif
  28.  
  29. #ifdef __MEMORY__
  30.     #include <Memory.h>
  31. #endif
  32.  
  33. #ifndef __ERRORS__
  34.     #include <Errors.h>
  35. #endif
  36.  
  37.  
  38. #ifndef __STREAMS__
  39.     #include "Streams.h"
  40. #endif
  41.  
  42.  
  43.  
  44.  
  45.  
  46. OSErr NewStream ( tStreamRef* streamRef, Size blockSize )
  47. {
  48.     OSErr                theErr;
  49.     Ptr                 thePtr;
  50.     tStreamHeaderPtr    theHeader;
  51.     
  52.     
  53.     *streamRef = nil;
  54.     thePtr = NewPtr ( sizeof ( tStreamHeader ) + blockSize );
  55.     theErr = MemError ( );
  56.     if ( theErr )
  57.         return theErr;
  58.     
  59.     theHeader = (tStreamHeaderPtr) thePtr;
  60.     theHeader->blockSize = blockSize;
  61.     theHeader->currentSize = sizeof ( tStreamHeader ) + blockSize;
  62.     theHeader->cursor = sizeof ( tStreamHeader );
  63.     theHeader->maxCursor = sizeof ( tStreamHeader );
  64.     
  65.     *streamRef = thePtr;
  66.     
  67.     return noErr;
  68. }
  69.  
  70.  
  71.  
  72. OSErr DisposeStream ( tStreamRef streamRef )
  73. {
  74.     OSErr    theErr;
  75.     
  76.     DisposePtr ( streamRef );
  77.     theErr = MemError ( );
  78.     if ( theErr == memWZErr )
  79.         return kInvalidStreamRef;
  80.     
  81.     return theErr;
  82. }
  83.  
  84.  
  85.  
  86. OSErr SetStreamData ( tStreamRef streamRef, Ptr dataPtr, Size dataSize )
  87. {
  88.     OSErr                theErr;
  89.     tStreamHeaderPtr    theHeader;
  90.     
  91.     theHeader = (tStreamHeaderPtr) streamRef;
  92.     
  93.     /* Allocate more memory if we need to */
  94.     if ( theHeader->cursor + dataSize > theHeader->currentSize )
  95.     {
  96.         SetPtrSize ( streamRef, theHeader->currentSize + theHeader->blockSize );
  97.         theErr = MemError ( );
  98.         if ( theErr )
  99.             return theErr;
  100.         
  101.         theHeader->currentSize += theHeader->blockSize;
  102.     }
  103.     
  104.     if ( dataPtr )
  105.         BlockMoveData ( dataPtr, streamRef + theHeader->cursor, dataSize );
  106.     theHeader->cursor += dataSize;
  107.     
  108.     if ( theHeader->cursor > theHeader->maxCursor )
  109.         theHeader->maxCursor = theHeader->cursor;
  110.     
  111.     return noErr;
  112. }
  113.  
  114.  
  115.  
  116. OSErr GetStreamData ( tStreamRef streamRef, Ptr dataPtr, Size dataSize )
  117. {
  118.     tStreamHeaderPtr    theHeader;
  119.     
  120.     theHeader = (tStreamHeaderPtr) streamRef;
  121.     if ( theHeader->cursor + dataSize > theHeader->maxCursor )
  122.         return kBoundsErr;
  123.         
  124.     BlockMoveData ( streamRef + theHeader->cursor, dataPtr, dataSize );
  125.     theHeader->cursor += dataSize;
  126.     
  127.     return noErr;
  128. }
  129.  
  130.  
  131.  
  132. OSErr GetStreamDataPtr ( tStreamRef streamRef, Ptr* dataPtr )
  133. {
  134.     OSErr                theErr;
  135.     Ptr                    thePtr;
  136.     Size                theSize;
  137.     tStreamHeaderPtr    theHeader;
  138.     
  139.     *dataPtr = nil;
  140.     theHeader = (tStreamHeaderPtr) streamRef;
  141.     theSize = GetStreamDataSize ( streamRef );
  142.     
  143.     thePtr = NewPtr ( theSize );
  144.     theErr = MemError ( );
  145.     if ( theErr )
  146.         return theErr;
  147.         
  148.     BlockMoveData ( streamRef + sizeof ( tStreamHeader ), thePtr, theSize );
  149.     *dataPtr = thePtr;
  150.     
  151.     return noErr;
  152. }
  153.  
  154.  
  155.  
  156. Size GetStreamDataSize ( tStreamRef streamRef )
  157. {
  158.     tStreamHeaderPtr    theHeader;
  159.     
  160.     theHeader = (tStreamHeaderPtr) streamRef;
  161.     return theHeader->maxCursor - sizeof ( tStreamHeader );
  162. }
  163.  
  164.  
  165.  
  166. tStreamCursor GetStreamCursor ( tStreamRef streamRef )
  167. {
  168.     tStreamHeaderPtr    theHeader;
  169.     
  170.     theHeader = (tStreamHeaderPtr) streamRef;
  171.     return theHeader->cursor;
  172. }
  173.  
  174.  
  175.  
  176. OSErr SetStreamCursor ( tStreamRef streamRef, tStreamCursor cursor )
  177. {
  178.     tStreamHeaderPtr    theHeader;
  179.     
  180.     theHeader = (tStreamHeaderPtr) streamRef;
  181.     if ( cursor > theHeader->currentSize )
  182.         return kBoundsErr;
  183.     if ( cursor < sizeof ( tStreamHeader ) )
  184.         return kBoundsErr;
  185.         
  186.     theHeader->cursor = cursor;
  187.     
  188.     return noErr;
  189. }
  190.  
  191.  
  192.  
  193. void ResetStreamCursor ( tStreamRef streamRef )
  194. {
  195.     tStreamHeaderPtr    theHeader;
  196.     
  197.     theHeader = (tStreamHeaderPtr) streamRef;
  198.     theHeader->cursor = sizeof ( tStreamHeader );
  199.     
  200.     return;
  201. }
  202.  
  203.  
  204.  
  205. OSErr CompactStream ( tStreamRef streamRef )
  206. {
  207.     OSErr                theErr;
  208.     tStreamHeaderPtr    theHeader;
  209.     
  210.     theHeader = (tStreamHeaderPtr) streamRef;
  211.     SetPtrSize ( streamRef, theHeader->maxCursor );
  212.     theErr = MemError ( );
  213.     if ( theErr == memWZErr )
  214.         return kInvalidStreamRef;
  215.     
  216.     theHeader->currentSize = theHeader->maxCursor;
  217.     
  218.     return theErr;
  219. }
  220.  
  221.  
  222.  
  223.  
  224.  
  225.